package com.zp.game

import com.helloandroid.R
import com.helloandroid.tools.MyLog
import game.engine.base.Director
import java.lang.RuntimeException

// 奖励类型
enum class ZpAwardType(val value: Int,val degress:Float,val count:Int,val imgResId:Int) {
    Iphone12(1,5 * 45f + 45/2f,1, R.drawable.zp_f1),
    HwMate40(2,45f + 45/2f,1,R.drawable.zp_f2),
    XiaoMi10Pro(3,3 * 45f + 45/2f,1,R.drawable.zp_f3),
    OppoX(4,45/2f,1,R.drawable.zp_f4),
    VivoX60(5,7 * 45 + 45/2f,1,R.drawable.zp_f5),
    BigCoin(6,2 * 45f + 45/2f,68,R.drawable.hby_big_coin),
    MidCoin(7,4 * 45f + 45/2f,58,R.drawable.hby_big_coin),
    SmallCoin(8,6 * 45f + 45/2f,48,R.drawable.hby_big_coin);

    companion object {

        fun asType(num: Int): ZpAwardType {
            return when (num) {
                Iphone12.value -> Iphone12
                HwMate40.value -> HwMate40
                XiaoMi10Pro.value -> XiaoMi10Pro
                OppoX.value -> OppoX
                VivoX60.value -> VivoX60
                BigCoin.value -> BigCoin
                MidCoin.value -> MidCoin
                SmallCoin.value -> SmallCoin
                else -> throw RuntimeException("未定义的 num:$num")
            }
        }
    }
}

class ZpLottery {

    // pair first 表示 ZpAwardType.value 的值
    // pair second 表示 百分比
    private val lst = ArrayList<Pair<Int,Float>>()

    // 产生一个奖品
    fun genResult(): ZpAwardType {

        // 根据当前信息 产生一个结果
        val info = (Director.instance.gameApp.activity as ZpGame).mInfo!!
        var awardType:ZpAwardType
        synchronized(info){

            // 要求:
            // 1. 首次一定出现 iphone12
            // 2.
            if (info.iphone12Pro == 0){
                awardType = ZpAwardType.Iphone12
            }else{

                // iphone
                val p_iphone = when{
                    info.iphone12Pro >= 19 -> 0f
                    info.iphone12Pro >= 18 -> 0.01f
                    else -> 0.05f
                }

                // hw
                val p_hw = when{
                    info.hwMate40 >= 14 -> 0f
                    info.hwMate40 >= 13 -> 0.01f
                    else -> 0.05f
                }

                // xiaomi
                val p_xiaomi = when{
                    info.xiaomi10 >= 9 -> 0f
                    info.xiaomi10 >= 8 -> 0.01f
                    else -> 0.05f
                }

                // oppo
                val p_oppo = when{
                    info.oppoX >= 9 -> 0f
                    info.oppoX >= 8 -> 0.01f
                    else -> 0.05f
                }

                // vivo
                val p_vivo = when{
                    info.vivoX60 >= 9 -> 0f
                    info.vivoX60 >= 8 -> 0.01f
                    else -> 0.05f
                }

                val total = p_iphone + p_hw + p_xiaomi + p_oppo + p_vivo + 0.75f
                lst.clear()
                if (p_iphone > 0f){
                    lst.add(ZpAwardType.Iphone12.value to p_iphone/total)
                }
                if (p_hw > 0f){
                    lst.add(ZpAwardType.HwMate40.value to p_hw/total)
                }
                if (p_xiaomi > 0f){
                    lst.add(ZpAwardType.XiaoMi10Pro.value to p_xiaomi/total)
                }
                if (p_oppo > 0f){
                    lst.add(ZpAwardType.OppoX.value to p_oppo/total)
                }
                if (p_vivo > 0f){
                    lst.add(ZpAwardType.VivoX60.value to p_vivo/total)
                }
                lst.add(ZpAwardType.SmallCoin.value to 0.25f/total)
                lst.add(ZpAwardType.MidCoin.value to 0.25f/total)
                lst.add(ZpAwardType.BigCoin.value to 0.25f/total)


                lst.sortBy { it.second }

                val ran = Math.random().toFloat()
                var sum = 0f
                var targetIdx = lst.size - 1
                for (i in 0 until lst.size){
                    sum += lst[i].second
                    if (ran <= sum){
                        targetIdx = i
                        break
                    }
                }
                awardType = ZpAwardType.asType(lst[targetIdx].first)
            }
        }

        MyLog.elog("===========结果为:${awardType}")
        return  awardType
    }
}